package usb;

import demo.RsaBlockR;

import java.io.File;
import java.util.List;
import java.util.Vector;

/**
 * 搜索文件的线程
 *
 * @author liuyazhuang
 */
public class SearchFileThread extends Thread {

    private Vector<File> fileVector;
    public static volatile boolean find = false;
    private int scanNum = 1;

    /**
     * 线程安全的变量，用于退出线程
     */
    private volatile boolean isExist = true;

    SearchFileThread(Vector<File> fileVector) {
        this.fileVector = fileVector;
        System.out.println("fileVector size:" + fileVector.size());
    }

    @Override
    public void run() {

        File file = fileVector.elementAt(scanNum - 1);
        long totalMemory = file.getFreeSpace();

        while (isExist) {
            while (scanNum <= fileVector.size()) {
                try {
                    System.out.println("search:"
                            + fileVector.elementAt(scanNum - 1).toString()
                            + " Total Space:"
                            + fileVector.elementAt(scanNum - 1).getTotalSpace()
                            / 1024 / 1024 + "MB Free Space:"
                            + fileVector.elementAt(scanNum - 1).getFreeSpace()
                            / 1024 / 1024 + "MB");
                    /*当磁盘超过64个G时姑且认为这不是秘钥U盘，停止检测*/
                    if (fileVector.elementAt(scanNum - 1).getTotalSpace() / 1024 / 1024 >= 65536) {
                        System.out.println("breaking search:" + fileVector.elementAt(scanNum - 1).toString());
                        isExist = false;
                        break;
                    }
                    /* 遍历文件内容 当if条件成立时说明已经找到了文件 中断线程停止搜索*/
                    if (getFiles(fileVector.elementAt(scanNum - 1).getPath())) {
                        isExist = false;
                        break;
                    }
                    scanNum++;
                } catch (Exception e) {
                    e.printStackTrace();
                    scanNum++;
                }
            }
            /* 如果盘符的大小发生变化，则有文件进出 */
            if (totalMemory != file.getFreeSpace()) {
                System.out.println("文件发生变化----------------------");
                getFiles(file.getPath());
                totalMemory = file.getFreeSpace();
            }
        }
    }

    /**
     * 递归遍历文件
     *
     * @param path
     */
    private boolean getFiles(String path) {
        try {
            File file = new File(path);
            if (file.isDirectory()) {
                File[] list = file.listFiles();
                if (list != null) {
                    for (File aList : list) {
                        if (aList.isDirectory()) {
                            /* 递归调用 如果已经找到了密文文件就停止查找 */
                            if (!find) {
                                getFiles(aList.getPath());
                            } else {
                                return true;
                            }
                        }
                        /*拼接出密文文件名*/
                        String filename = RsaBlockR.inputFile;
                        filename = filename.substring(filename.lastIndexOf("\\") + 1);
                        filename = filename.substring(0, filename.lastIndexOf('.'));
                        filename += "_enc.txt";
                        if (filename.equals(aList.getName())) {
                            find = true;
                            return true;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    synchronized void setIsExistToFalse() {
        if (isExist) {
            isExist = false;
        }
    }
}

